﻿using Modelado.Modulos.ClasesModelado;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
    public class AD_Empresa
    {
        public void CRE_Empresa(SqlCommand comando, MOD_Empresa obj)
        {
            try
            {
                string sp = "SP_CRE_Empresa";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@EMP_CODOWNER", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_CODBASEDATOS", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_CODODBC", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_CODESQUEMAPLANTA", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_NOMBRE", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.EMP_codowner;
                comando.Parameters[1].Value = obj.EMP_codbasedatos;
                comando.Parameters[2].Value = obj.EMP_cododbc;
                comando.Parameters[3].Value = obj.EMP_codeesquemaplanta;
                comando.Parameters[4].Value = obj.EMP_nombre;

                comando.ExecuteNonQuery();
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public void DEL_Empresa(SqlCommand comando, string codigo)
        {
            try
            {
                string sp = "SP_DEL_Empresa";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@EMP_CODOWNER", SqlDbType.VarChar));

                comando.Parameters[0].Value = codigo;

                comando.ExecuteNonQuery();

            }
            catch (Exception exc)
            {

                throw exc;
            }
        }


        public void UPD_Empresa(SqlCommand comando, MOD_Empresa obj)
        {
            try
            {
                string sp = "SP_UPD_Empresa";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@EMP_CODOWNER", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_CODBASEDATOS", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_CODODBC", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_CODESQUEMAPLANTA", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@EMP_NOMBRE", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.EMP_codowner;
                comando.Parameters[1].Value = obj.EMP_codbasedatos;
                comando.Parameters[2].Value = obj.EMP_cododbc;
                comando.Parameters[3].Value = obj.EMP_codeesquemaplanta;
                comando.Parameters[4].Value = obj.EMP_nombre;

                comando.ExecuteNonQuery();
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }


        public List<MOD_Empresa> SEL_Grid_Empresa(SqlCommand comando)
        {
            try
            {
                string sp = "SP_SEL_Grid_Empresa";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_Empresa> lista = new List<MOD_Empresa>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_Empresa obj = new MOD_Empresa();
                    obj.EMP_codowner = Convert.ToInt32(dt.Rows[i]["EMP_codowner"]);
                    obj.EMP_nombre = dt.Rows[i]["EMP_nombre"].ToString();
                    obj.EMP_codbasedatos = Convert.ToInt32(dt.Rows[i]["EMP_codbasedatos"]);
                    obj.BDD_nombre = dt.Rows[i]["BDD_nombrebd"].ToString();
                    obj.EMP_cododbc = Convert.ToInt32(dt.Rows[i]["EMP_cododbc"]);
                    obj.ODBC_nombre = dt.Rows[i]["ODBC_nombre"].ToString();
                    obj.EMP_codeesquemaplanta = Convert.ToInt32(dt.Rows[i]["EMP_codesquemaplanta"]);
                    obj.ESQ_nombreesqplanta = dt.Rows[i]["ESQ_nombreesqplanta"].ToString();
                    obj.EMP_owner = dt.Rows[i]["EMP_owner"].ToString();
                    lista.Add(obj);
                }

                return lista;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public MOD_Empresa SEL_EmpresaByID(SqlCommand comando, int codigo)
        {
            try
            {
                string sp = "SP_SEL_EmpresaByID";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@EMP_codowner", SqlDbType.VarChar));

                comando.Parameters[0].Value = codigo;

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_Empresa obj = new MOD_Empresa();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    obj.EMP_codowner = Convert.ToInt32(dt.Rows[i]["EMP_codowner"]);
                    obj.EMP_codbasedatos = Convert.ToInt32(dt.Rows[i]["EMP_codbasedatos"]);
                    obj.EMP_cododbc = Convert.ToInt32(dt.Rows[i]["EMP_cododbc"]);
                    obj.EMP_codeesquemaplanta = Convert.ToInt32(dt.Rows[i]["EMP_codesquemaplanta"]);
                    obj.EMP_nombre = dt.Rows[i]["EMP_nombre"].ToString();
                    obj.EMP_owner = dt.Rows[i]["EMP_owner"].ToString();
                }

                return obj;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public int SEL_EmpresaByNombre(SqlCommand comando, string nombre)
        {
            try
            {
                string sp = "SP_SEL_EmpresaByNOmbre";
                int cant = 0;
                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@EMP_NOMBRE", SqlDbType.VarChar));

                comando.Parameters[0].Value = nombre;

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_Empresa obj = new MOD_Empresa();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cant = Convert.ToInt32(dt.Rows[i]["EMPRESA_CANT"]);
                }

                return cant;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }
    }


}
